import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.logging.Logger;
import java.lang.reflect.Proxy;

/**
 * 动态代理模式模式
 * DynamicProxy
 */
public class DynamicProxy {

    public static void main(String[] args) {
        var jdkProxy = new JdkProxy(new Ukraine());
        jdkProxy.getInstance().attack();
    }
}


class Ukraine implements NATO {

    @Override
    public void attack() {
        Logger.getGlobal().info("launch");
    }
}

interface NATO {
    void attack();
}

/**
 * JDK实现InvocationHandler
 */
class JdkProxy implements InvocationHandler {

    private Ukraine target;

    public JdkProxy(Ukraine target) {
        this.target = target;
    }

    public NATO getInstance() {
        return (NATO) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Logger.getGlobal().info("search Russia");
        var object = method.invoke(target, args);
        Logger.getGlobal().info("destroy");
        return object;
    }
}





